package com.cloudclassroom.controller;

import com.cloudclassroom.vo.RestResult;
import jakarta.servlet.ServletException;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;
import org.springframework.web.servlet.NoHandlerFoundException;

/**
 * @author 92110
 */
@RestControllerAdvice
public class ExceptionController {
    @ExceptionHandler(Exception.class)
    public RestResult<String> error(Exception e){
        if(e instanceof NoHandlerFoundException) {
            return RestResult.failure(404, e.getMessage());
        } else if (e instanceof ServletException) {
            //其他的Servlet异常就返回400状态码
            return RestResult.failure(400, e.getMessage());
        } else {
            //其他异常直接返回500
            return RestResult.failure(500, e.getMessage());
        }
    }
}
